package com.example.myapplication3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

public class MyTask<handler> extends AppCompatActivity implements Runnable {

    private static final String TAG = "MyTask";
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_task);
    }

    public void run() {
        Log.i(TAG, "run:........");

        //线程中完成的任务。。。。。
        // URL url = null;
        List<String> ret = new ArrayList<String>();
        try {
            Thread.sleep(3000);
            Document doc = Jsoup.connect("https://www.usd-cny.com/bankofchina.htm").get();
            Log.i(TAG, "run:title=" + doc.title());
            Element publicTime = doc.getElementsByClass("time").first();
            Log.i(TAG, "run:time=" + publicTime.html());
            Element table = doc.getElementsByTag("table").first();
            Elements trs = table.getElementsByTag("tr");   //Elements集合，Element单个
            for (Element tr : trs) {
                Elements tds = tr.getElementsByTag("td");
                if (tds.size() > 0) {
                    Log.i(TAG, "run td=" + tds.first().text());
                    Log.i(TAG, "run rate=" + tds.get(5).text());
                    //将获取数据传到列表中去
                    String str = tds.first().text();
                    Log.i(TAG, "run:td=" + str);
                    String val = tds.get(5).text();
                    Log.i(TAG, "run:rate=" + val);
                    ret.add(str + "-->" + val);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //返回数据给主线程

        Message msg = handler.obtainMessage(9);
        msg.obj = "from message";
        handler.sendMessage(msg);

        //
    }

    public void setHandler(Handler handler) {
    }
}